package com.yunpai.thread;

import java.util.concurrent.Callable;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 创建线程池
 */
public class ExecutorServiceThread {

    static class Thread1 implements Runnable {

        @Override
        public void run() {
            Thread.currentThread().setName("线程一");
            for (int i =1 ; i <=100; i++) {
                if (i % 2 == 0) {
                    System.out.println(Thread.currentThread().getName() + ":" + i);
                }
            }
        }
    }

    static class Thread2 implements Runnable {

        @Override
        public void run() {
            Thread.currentThread().setName("线程二");
            for (int i = 1; i <=100; i++) {
                if (i % 100 != 0) {
                    System.out.println(Thread.currentThread().getName() + ":" + i);
                }
            }
        }
    }

    public static void main(String[] args) {
        //1.提供指定线程数量的线程池
        ExecutorService service = Executors.newFixedThreadPool(10);
        //2.执行指定的线程操作,需要提供Runnable 接口或callable接口实现类的对象
        service.execute(new Thread1());
        service.execute(new Thread2());
       // service.submit(Callable callable); //需要提供Callable接口
        //关闭线程池
        service.shutdown();
    }
}
